package com.example.animaltiontext_1;


import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class AnimationActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView mImage;
    private Button mAlphaButton;
    private Button mRotateButton;
    private Button mScaleButton;
    private Button mTranslateButton;
    private Button mSetButton;
    private TextView mText1;
    private TextView mText2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        mImage = findViewById(R.id.image);
        mRotateButton = findViewById(R.id.btn_rotate);
        mAlphaButton = findViewById(R.id.btn_alpha);
        mScaleButton = findViewById(R.id.btn_scale);
        mTranslateButton = findViewById(R.id.btn_trandlate);
        mSetButton = findViewById(R.id.btn_set);
        mText1 = findViewById(R.id.text1);
        mText2 = findViewById(R.id.text2);
        mTranslateButton.setOnClickListener(this);
        mAlphaButton.setOnClickListener(this);
        mRotateButton.setOnClickListener(this);
        mScaleButton.setOnClickListener(this);
        mSetButton.setOnClickListener(this);
        mText1.setOnClickListener(this);
        mText2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_alpha) {
            //startAlphaAnimXML();

            startAlphaAnimJava();
        } else if (id == R.id.btn_rotate) {
//            startRotateAnimXML();
            startTotateAnimJava();
        }else if (id == R.id.btn_scale) {
//            startScaleAnimXML();
            startScaleAnimalJava();
        }else if (id == R.id.btn_trandlate){
            startTranslateAnimXML();
//            startTranslateAnimJava();
        } else if (id == R.id.btn_set) {
//            startSetAnimXML();
            stratSetAnimJava();
        } else if ( id== R.id.text2) {
            startText2Anim();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        //overridePendingTransition(R.anim.no_anim, R.anim.alpha_out);
    }

    //    活动连续
    private  void  startText2Anim(){
        AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
        alphaAnimation.setDuration(1000);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                startText1Anim();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mText2.startAnimation(alphaAnimation);
    }
    private void startText1Anim(){
        ScaleAnimation scaleAnimation = new ScaleAnimation(
                1,2,1,1
        );
        scaleAnimation.setDuration(1000);
        scaleAnimation.setFillAfter(true);
        mText1.startAnimation(scaleAnimation);
    }
// 组合
    private void startSetAnimXML(){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.animations);
        mImage.startAnimation(animation);
    }

    private void stratSetAnimJava(){
        TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.ABSOLUTE,0,
                Animation.ABSOLUTE,0,
                Animation.ABSOLUTE,0,
                Animation.RELATIVE_TO_PARENT,0.5F);
        RotateAnimation rotateAnimation = new RotateAnimation(0,180,
                Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_PARENT,0.5f);
        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(translateAnimation);
        animationSet.addAnimation(rotateAnimation);
        animationSet.setDuration(1000);
        mImage.startAnimation(animationSet);
    }


//平移
    private  void  startTranslateAnimXML(){
        Animation translateAnimation = AnimationUtils.loadAnimation(this,R.anim.translate);
        mImage.startAnimation(translateAnimation);
    }
    private void startTranslateAnimJava(){
//        TranslateAnimation translateAnimation = new TranslateAnimation(
//                0,200,0,0);
        TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.ABSOLUTE,0,
                Animation.ABSOLUTE,0,
                Animation.ABSOLUTE,0,
                Animation.RELATIVE_TO_PARENT,0.5F);
        translateAnimation.setDuration(1000);
        translateAnimation.setInterpolator(new AnticipateInterpolator());
        mImage.startAnimation(translateAnimation);
    }

    private void startScaleAnimXML(){
        Animation scaleAnimation = AnimationUtils.loadAnimation(this,R.anim.scale);
        scaleAnimation.setRepeatCount(5);
        scaleAnimation.setRepeatMode(Animation.REVERSE);
        mImage.startAnimation(scaleAnimation);

    }

    private void startScaleAnimalJava(){
//        参数为X和Y的初始和结束缩放比例，支点默认为（0，0）
//        ScaleAnimation scaleAnimation = new ScaleAnimation(1,2,1,2);
//        前四个参数为X和Y的初始和结束缩放比例，后两个参数为指点相对于（0，0）点的距离
//        ScaleAnimation scaleAnimation = new ScaleAnimation(1,2,1,2,100,200);
//        前四个参数为X和Y的初始和结束缩放比例，后四个参数为指点类型和对应值
        ScaleAnimation scaleAnimation = new ScaleAnimation(
                1,2,1,2,
                Animation.RELATIVE_TO_SELF,0.5F,
                Animation.RELATIVE_TO_PARENT,0F);
//        单词播放时长为2秒
        scaleAnimation.setDuration(1000);
//        动画播放次数
        scaleAnimation.setRepeatCount(4);
//        动画播放模式为REVERS
        scaleAnimation.setRepeatMode(Animation.REVERSE);
//        设定动画播放结束后保持之后效果
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setInterpolator(new BounceInterpolator());
//        对minage开始博凡动画anim
        mImage.startAnimation(scaleAnimation);
    }
    private void startAlphaAnimXML() {
        Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
        mImage.startAnimation(alphaAnimation);
    }
    private void startRotateAnimXML() {
        Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate);
        mImage.startAnimation(alphaAnimation);
    }

    private void startTotateAnimJava(){
//        RotateAnimation rotateAnimation = new RotateAnimation(0,180);
//        RotateAnimation rotateAnimation = new RotateAnimation(0,180, 100,100);

        RotateAnimation rotateAnimation = new RotateAnimation(0,180,
                Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_PARENT,0.5f);
        rotateAnimation.setDuration(1000);
//        rotateAnimation.setRepeatCount(3);
//        rotateAnimation.setRepeatMode(Animation.REVERSE);
        rotateAnimation.setInterpolator(new BounceInterpolator());
        rotateAnimation.setFillAfter(true);
        mImage.startAnimation(rotateAnimation);
    }
    private void startAlphaAnimJava() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0F, 0.5F);
        alphaAnimation.setDuration(1000);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setRepeatCount(5);
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        alphaAnimation.setInterpolator(new BounceInterpolator());
        mImage.startAnimation(alphaAnimation);
    }
}